package vrp;

import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;

import java.util.Date;

import java.io.IOException;
import javax.jdo.PersistenceManager;
import javax.jdo.Query;
import javax.servlet.http.*;

import vrp.PMF;
import vrp.Customer;
import vrp.Distance;

public class CustomerEditServlet extends HttpServlet {

    public void doPost(HttpServletRequest req, HttpServletResponse resp)
                throws IOException {
    	
    	PersistenceManager pm = PMF.get().getPersistenceManager();
    	
        long keyid = Integer.parseInt(req.getParameter("key"));
        String name = req.getParameter("name");
        String address = req.getParameter("address");
        String postalCode = req.getParameter("postalCode");
        String contactNo = req.getParameter("contactNo");
        double lat = Float.parseFloat(req.getParameter("lat"));
        double lng = Float.parseFloat(req.getParameter("lng"));
        String distances = req.getParameter("distances");

        Key key = KeyFactory.createKey(Customer.class.getSimpleName(), keyid);
        Customer customer = pm.getObjectById(Customer.class, key);
        boolean isDepot = customer.getIsDepot();
        String respondRedirect = "";
        if (isDepot == true) {
        	respondRedirect = "/depots.jsp";
        } else {
        	respondRedirect = "/customers.jsp";
        }

        try {
            customer.setName(name);
            customer.setAddress(address);
            customer.setContactNo(contactNo);
            
            String oldPostalCode = customer.getPostalCode();
            if (!oldPostalCode.equals(postalCode)){
            	//if change postal code, need to update position and distances
                customer.setPostalCode(postalCode);
                customer.setLat(lat);
                customer.setLng(lng);
                
                if (!distances.equals("")){
                	long customerID = customer.getKey().getId();
                	
                	Query query1 = pm.newQuery(Distance.class);
                    query1.setFilter("from == customerIDpara");
                    query1.declareParameters("long customerIDpara");
                    query1.deletePersistentAll(customerID);
                	Query query2 = pm.newQuery(Distance.class);
                    query2.setFilter("to == customerIDpara");
                    query2.declareParameters("long customerIDpara");
                    query2.deletePersistentAll(customerID);
                    
                    String[] customers = distances.split("-");
    	            for (String c: customers){
    	            	String[] components = c.split(" ");
    	            	int cid = Integer.parseInt(components[0]);
    	            	if (cid!=customerID){
	    	            	double from = Float.parseFloat(components[1]);
	    	            	double to = Float.parseFloat(components[2]);
	    	            	Distance distance1 = new Distance(customerID,cid,to);
	    	            	Distance distance2 = new Distance(cid,customerID,from);
	    	            	pm.makePersistent(distance1);
	    	            	pm.makePersistent(distance2);
    	            	}
    	            }
                }
            }
            customer.setModifiedAt(new Date());
            pm.makePersistent(customer);
        } finally {
            pm.close();
        }
        resp.sendRedirect(respondRedirect);
    }
}
